package com.sbl.bajw.common.web;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

public class SessionUnit{
	 
	public static final String sessionIdParamerName = "HttpSessionId";
	
	static HashMap<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();
	
	public static void addSession(String sessionId,HttpSession session)
	{
		if(sessionMap == null)
		{
			sessionMap = new HashMap<String, HttpSession>();
		}
		sessionMap.put(sessionId, session);
	}
	
	public static HttpSession getSession(String sessionId)
	{
		if(sessionMap == null)
		{
			sessionMap = new HashMap<String, HttpSession>();
		}
		HttpSession session = sessionMap.get(sessionId);
		return session;
	}
	
	public static void removeSession(String sessionId)
	{
		if(sessionMap == null)
		{
			sessionMap = new HashMap<String, HttpSession>();
		}
		sessionMap.remove(sessionId);
	}
	
	public static HttpSession getSession()
	{
		String sessionId = RequestUnit.getString(sessionIdParamerName);
		HttpSession session = null;
		if(sessionId != null && !sessionId.equals(""))
		{
			session = getSession(sessionId);			
		}
		
		if(session == null)
		{
			session = WebContext.getRequest().getSession();
		}
		return session;
	}
	
	public static String getSessionId()
	{
		HttpSession session = getSession();
		if(session == null)
		{
			return null;
		}
		String sessionId = session.getId();
		return sessionId;
	}
	
	public static String getUserId() 
	{
		HttpSession session = getSession();
		
		if(session == null)
		{
			return null;
		}
		
		Object value =  session.getAttribute("userId");
		if(value == null)
		{
			return null;
		}
		else
		{
			String userId = session.getAttribute("userId") + "";
			return userId;
		}		
	}
	
	public static String getDeptId() 
	{
		HttpSession session = getSession();
		
		if(session == null)
		{
			return null;
		}
		
		Object value =  session.getAttribute("deptId");
		if(value == null)
		{
			return null;
		}
		else
		{
			String deptId = session.getAttribute("deptId") + "";
			return deptId;
		}		
	}
	
	public static String getUserName()
	{
		HttpSession session = getSession();
		
		if(session == null)
		{
			return null;
		}
		
		Object value =  session.getAttribute("userName");
		if(value == null)
		{
			return "";
		}
		else
		{
			String userName = session.getAttribute("userName") + "";
			return userName;
		}		
	}
	
	public static void setCity(String cityName)
	{
		HttpSession session = getSession();
		session.setAttribute("city", cityName);	
	}
	
	public static String getCity()
	{
		HttpSession session = getSession();
		
		if(session == null)
		{
			return null;
		}
		
		Object value =  session.getAttribute("city");
		if(value == null)
		{
			return "";
		}
		else
		{
			String city = session.getAttribute("city") + "";
			return city;
		}		
	}
	
	public static void setUserId(String userId)
	{
		HttpSession session = getSession();
		session.setAttribute("userId", userId);
	}
	
	public static void setUserName(String userName)
	{
		HttpSession session = getSession();
		session.setAttribute("userName", userName);
	}
	
	public static void setDeptId(String deptId)
	{
		HttpSession session = getSession();
		session.setAttribute("deptId", deptId);
	}
	
	public static void setValue(String key,Object value)
	{
		HttpSession session = getSession();
		session.setAttribute(key, value);
	}
	
	public static String getValue(String key)
	{
		HttpSession session = getSession();
		
		if(session == null)
		{
			return null;
		}
		
		Object value =  session.getAttribute(key);
		if(value == null)
		{
			return "";
		}
		else
		{
			String userName = session.getAttribute(key) + "";
			return userName;
		}		
	}
}